package com.yupi.springbootinit.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yupi.springbootinit.constant.CommonConstant;
import com.yupi.springbootinit.mapper.ChartInfoMapper;
import com.yupi.springbootinit.model.dto.chartInfo.ChartInfoQueryRequest;
import com.yupi.springbootinit.model.entity.*;
import com.yupi.springbootinit.model.vo.ChartInfoVO;
import com.yupi.springbootinit.model.vo.PostVO;
import com.yupi.springbootinit.service.ChartInfoService;
import com.yupi.springbootinit.utils.SqlUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static com.yupi.springbootinit.constant.UserConstant.USER_LOGIN_STATE;

/**
 * @author lfc
 * @description 针对表【chartinfo(图表信息表)】的数据库操作Service实现
 * @createDate 2023-09-01 20:13:22
 */
@Service
public class ChartInfoServiceImpl extends ServiceImpl<ChartInfoMapper, ChartInfo>
        implements ChartInfoService {

    @Override
    public QueryWrapper<ChartInfo> getQueryWrapper(ChartInfoQueryRequest chartInfoQueryRequest, HttpServletRequest request) {
        QueryWrapper<ChartInfo> queryWrapper = new QueryWrapper<>();
        if (chartInfoQueryRequest == null) {
            return queryWrapper;
        }
        //查询当前登陆用户的ID
        User user = (User) request.getSession().getAttribute(USER_LOGIN_STATE);
        Long userId = user.getId();
        String chartName = chartInfoQueryRequest.getName();
        String sortField = chartInfoQueryRequest.getSortField();
        String sortOrder = chartInfoQueryRequest.getSortOrder();
        //拼接查询条件
        if (StringUtils.isNotBlank(chartName)) {
            queryWrapper.like("name", chartName);
        }
        //根据当前登录用户查询
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("is_delete", false);
        //排序条件，先根据desc，
        queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);
        return queryWrapper;
    }
}




